package com.cowmooed.config;

import com.cowmooed.interceptor.JwtAuthInterceptor;
import com.cowmooed.interceptor.LogInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web配置类
 * 用于注册拦截器等Web相关配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private JwtAuthInterceptor jwtAuthInterceptor;
    
    @Autowired
    private LogInterceptor logInterceptor;

    /**
     * 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册日志拦截器（先执行）
        registry.addInterceptor(logInterceptor)
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns("/backcode/swagger-ui/**") // 排除Swagger UI接口
                .excludePathPatterns("/backcode/swagger-resources/**") // 排除Swagger资源
                .excludePathPatterns("/backcode/webjars/**") // 排除webjars资源
                .excludePathPatterns("/backcode/v2/api-docs/**") // 排除Swagger 2 API文档资源
                .excludePathPatterns("/backcode/v3/api-docs/**") // 排除OpenAPI 3 API文档资源
                .excludePathPatterns("/backcode/favicon.ico"); // 排除favicon
        
        // 注册JWT认证拦截器（后执行）
        registry.addInterceptor(jwtAuthInterceptor)
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns("/backcode/auth/**") // 排除认证相关接口
                .excludePathPatterns("/backcode/swagger-ui/**") // 排除Swagger UI接口
                .excludePathPatterns("/backcode/swagger-resources/**") // 排除Swagger资源
                .excludePathPatterns("/backcode/webjars/**") // 排除webjars资源
                .excludePathPatterns("/backcode/v2/api-docs/**") // 排除Swagger 2 API文档资源
                .excludePathPatterns("/backcode/v3/api-docs/**") // 排除OpenAPI 3 API文档资源
                .excludePathPatterns("/backcode/favicon.ico"); // 排除favicon
    }
}